#cp.strings

Provides strings from (potentially) multiple sources, with support for loading from multiple languages.

local strs = require("cp.strings").new():fromPlist("/Path/To/Resources/${language}.lproj/MYLocalization.strings") local value = strs:find("en", "AKey")

This will load the file for the specified language (replacing ${language} with "en" in the path) and return the value. Notes: This will load the file on each request. To have values cached, use the cp.strings module and specify a plist as a source.


#Submodules


#API Overview

Constructors - API calls which return an object, typically one that offers API methods

Methods - API calls which can only be made on an object returned by a constructor


#API Documentation

#Constructors

Signaturecp.strings.new(context) -> cp.strings
TypeConstructor
DescriptionCreates a new strings instance. You should add sources with the from or fromPlist methods.
Parameters
  • context - The initial context.
Returns
  • The new cp.strings
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 198

#Methods

Signaturecp.strings:context([context]) -> table | self
TypeMethod
DescriptionGets or sets a context to be set for the strings. This typically includes a language, which provides the default language code, but may have other source-specific properties. Calling this method may may clear caches, etc.
Parameters
  • context - A table with values which may be used by the source.
Returns
  • If a new context is provided, the cp.string.source is returned, otherwise the current context table is returned.
Notes
  • For example:
  • lua</li><li>string:context({language = "fr"}) -- set the default language to French.</li><li>
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 34

Signaturecp.strings:find(key[, context[, quiet]) -> string | nil
TypeMethod
DescriptionSearches for the specified key, caching the result when found.
Parameters
  • key - The key to retrieve from the file.
  • context - Optional table with additional/alternate context.
  • quiet - Optional boolean, defaults to false. If true, no warnings are logged for missing keys.
Returns
  • The value of the key, or nil if not found.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 130

Signaturecp.strings:findAllKeys([context]) -> table
TypeMethod
DescriptionSearches for all keys in all sources, with the given context.
Parameters
  • context - The intial context to use.
Returns
  • The array of keys, or {} if not found.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 185

Signaturecp.strings:findInSources(key[, context[, quiet]]) -> string | nil
TypeMethod
DescriptionSearches directly in the sources for the specified key.
Parameters
  • key - The key to retrieve from the file.
  • context - Optional table with additional/alternate context.
  • quiet - Optional boolean, defaults to false. If true, no warnings are logged for missing keys.
Returns
  • The value of the key, or nil if not found.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 92

Signaturecp.strings:findKeys(value[, context]) -> string | nil
TypeMethod
DescriptionSearches for the list of keys with a matching value, in the specified language.
Parameters
  • value - The value to search for.
  • context - The language code to look for (e.g. "en", or "fr").
Returns
  • The array of keys, or {} if not found.
Notes
  • Not recommended in production code, as it will potentially be very inefficient.
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 167

Signaturecp.strings:findKeysInSources(value[, context]) -> string | nil
TypeMethod
DescriptionSearches directly in the sources for the specified key value pattern.
Parameters
  • value - The value to search for.
  • context - Optional additional context for the request.
Returns
  • The array of keys, or {} if not found.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 111

Signaturecp.strings:from(source) -> cp.strings
TypeMethod
DescriptionAdds the source to the strings sources.
Parameters
  • source - The source to add.
Returns
  • The current cp.strings instance.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 63

Signaturecp.strings:fromPlist(pathPattern) -> cp.strings
TypeMethod
DescriptionConvenience method for adding a plist source to the strings instance.
Parameters
  • pathPattern - The path to load from. May contain a special ${language} marker which will be replace with the provided langauge when searching.
Returns
  • The current cp.strings instance.
NotesNone
ExamplesNone
Sourcesrc/extensions/cp/strings/init.lua line 79